package com.windfone.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.Constant;
import com.windfone.common.PageUtil;
import com.windfone.common.StringUtil;
import com.windfone.model.User;
import com.windfone.service.UserService;

public class UserAction extends ActionSupport {
	/**	serialVersionUID */
	private static final long serialVersionUID = 1L;
	private UserService userService;
	private int userId;
	private int level;
	private String name;
	private String password;
	private User bean;
	private int beginRecord;
	private int currentPage;
	private int nextPage;
	private int totalPage;
	private int prevPage;
	private String message;
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public User getBean() {
		return bean;
	}

	public void setBean(User bean) {
		this.bean = bean;
	}
	
	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPrevPage() {
		return prevPage;
	}

	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	/** page avaible area */
	private List<User> userList;
	
	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public int getBeginRecord() {
		return beginRecord;
	}

	public void setBeginRecord(int beginRecord) {
		this.beginRecord = beginRecord;
	}

	public int getUserId() {
		return userId;
	}
	
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public String addUser()throws Exception {
		User bean = new User();
		bean.setLevel(level);
		bean.setName(name);
		if ( password !=null && !password.equals("")){
			bean.setPassword( StringUtil.encode(password));
		}
		if (!userService.checkExistUser(name)){
			
			userService.insertUser(bean );
		}
		return SUCCESS;
	}
	public String deleteUser()throws Exception {
		userService.deleteUser(userId);
		return SUCCESS;
	}
	
	public String changePassword()throws Exception {
		if ( password !=null && !password.equals("")){
			bean = userService.getUserById(userId).get(0);
			bean.setPassword( StringUtil.encode(password));
			if (userService.updateUser(bean)==1)
				message="Success";
			else 
				message="fail";
		}
		return SUCCESS;
	}
	
	public String beforeChwd()throws Exception {
		String userName = "";
		message = "";
	    HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        if (session.getAttribute("userName")!=null){
        	userName = (String)session.getAttribute("userName");
        	bean = userService.getUserByName(userName);
        }
		return SUCCESS;
	}
	public String beforUpdateUser()throws Exception {
		bean = userService.getUserById(userId).get(0);
		return SUCCESS;
	}
	public String updateUser()throws Exception {
		User bean = new User();
		bean.setId(userId);
		bean.setLevel(level);
		bean.setName(name);
		if ( password !=null && !password.equals("")){
			bean.setPassword( StringUtil.encode(password));
		}
		userService.updateUser(bean);
		return SUCCESS;
	}
	public String userList() throws Exception{
		PageUtil pu = new PageUtil();
		pu.setCurrentPage(currentPage);
		int totalRecords = userService.getAllRecorderNumber() ;
		pu.setTotalRecords( totalRecords );
		pu.setPageSize(Constant.PAGE_SIZE);
		pu.dealPage();
		currentPage = pu.getCurrentPage();
		nextPage = pu.getNextPage();
		totalPage = pu.getTotalPage();
		prevPage = pu.getPrevPage();
		
		beginRecord = Constant.PAGE_SIZE * (currentPage -1);
		if ( beginRecord< 0 )
			beginRecord = 0; 
		userList = userService.getAllUser(beginRecord);
		return SUCCESS;
	}
	public static void main(String args[]){
		System.out.println( StringUtil.encode("1") );
	}
}
